package com.example.demo.controller;

import com.example.demo.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author liuyandeng
 * @date 2023/8/9
 * @Description
 */

@Controller
@ResponseBody // 返回一个非静态页面的数据
@RequestMapping(value = "/user") // 路由地址  method = RequestMethod.POST
public class UserController {

    @RequestMapping("/getSession") // localhost:8080/user/getSession
    public String getSession(@SessionAttribute(name = "name", required = false) String name) {
        return name;
    }

    @RequestMapping("/setSession") // localhost:8080/user/setSession
    public String setSession(String name, HttpServletRequest req) {
        HttpSession session = req.getSession(true); // 有Session就获取，没有就创建
        if (session != null) {
            session.setAttribute("name", name);
        }
        return "Session 设置成功！";
    }


    @RequestMapping("/getAgent") // localhost:8080/user/getAgent
    public String getAgent(@RequestHeader("User-Agent") String userAgent) {
        return userAgent;
    }

    @RequestMapping("/getCookie") // localhost:8080/user/getCookie
    public String getCookie(@CookieValue String name) {
        return "Cookie-name=" + name;
    }

    @RequestMapping("/upFile") // localhost:8080/user/upFile
    public String upFile(Integer id, @RequestPart("photo") MultipartFile file) throws IOException {
        //业务处理....
        // 1、生成唯一的id
        String fileName = UUID.randomUUID().toString(); // UUID是唯一id：时间轴+网卡号+随机数....
        // 2、获取上传文件名的后缀
        String fileType = file.getOriginalFilename().substring(
                file.getOriginalFilename().lastIndexOf(".") // lastIndexOf是以“."往下获取字符串
        ); // 获取后缀名
        fileName += fileType; // 组成完整的文件名
        file.transferTo(new File("E:\\" + fileName));
        return "id:" + id + " 图片上传成功！";
    }

    @RequestMapping("/loginByPath/{name}/{passwrod}") // localhost:8080/user/loginByPath/{name}/{passwrod}
    public String loginByPath(@PathVariable String name,
                              @PathVariable("passwrod") String pwd) {
        return "name= " + name + "| password=" + pwd;
    }

    @RequestMapping("/loginByJson") // localhost:8080/user/loginByJson
    public String loginByJson(@RequestBody User user) {
        return "name= " + user.getName() + "| password=" + user.getPassword();
    }

    @RequestMapping("/login3") //localhost:8080/user/login3
    public String login3(@RequestParam String name, @RequestParam(value = "pwd", required = false) String password) {
        return "name= " + name + "| password=" + password;
    }

    @RequestMapping("/login2") //localhost:8080/user/login2
    public String login2(@RequestParam String name, @RequestParam String password) {
        return "name= " + name + "| password=" + password;
    }

    @RequestMapping("/login") //localhost:8080/user/login
    public String login(String name, @RequestParam("pwd") String password) {
        return "name= " + name + "| password=" + password;
    }

    @RequestMapping("/add2") // localhost:8080/user/add2
    public String add2(User user) { // 这里就是拿到了这个对象
        return user.toString();
    }

    @RequestMapping("/add") // localhost:8080/user/add
    public String add(User user) { // 这里就是拿到了这个对象
        return user.toString();
    }

    @RequestMapping("/hi4") //localhost:8080/user/hi4
    public String hi4(String name) {
        return "Hello " + name;
    }

    @RequestMapping("/hi1") //localhost:8080/user/hi
    public String hi() {
        return "Hello ,Spring MVC1!!!";
    }

    @PostMapping("/hi2")
    public String hi2() {
        return "Hello ,Spring MVC2!!!";
    }

    @GetMapping("/hi3")
    public String hi3() {
        return "Hello ,Spring MVC2!!!";
    }
}
